package com.dk.controller;

import com.dk.pojo.Role;
import com.dk.pojo.User;
import com.dk.service.UserService;
import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.dc.pr.PRError;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("toshow")
    public String toshow() {
        return "user/show";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public List<User> getInfo() {
        List<User> a = userService.selectInfo();
        return a;

    }

    @RequestMapping("addInfo")
//增加获取也要获取角色信息
    public String get(Model de) {
        List<Role> role = userService.selctBRole();
        de.addAttribute("role", role);
        return "user/add";
    }

    @RequestMapping("insertinto")
    @ResponseBody
    public boolean insertinto(User user, Integer[] rid) {
        boolean a = userService.insert(user, rid);
        return a;
    }

    @RequestMapping("huixuan")
    public String select(Integer uid, Model mo) {
        User u = userService.selctBuid(uid);
        List<Role> role = userService.selecBrole();
        List<Integer> rid = userService.selectRid(uid);
        mo.addAttribute("user", u);
        mo.addAttribute("role", role);
        mo.addAttribute("rid", rid);
        return "user/update";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(User u, @RequestParam("rid") Integer[] rid) {
        boolean a = userService.updateInByRid(u, rid);
        return a;
    }

    @RequestMapping("deleteBy")
    @ResponseBody
    public boolean deleteBy(Integer id) {
        boolean a = userService.deleteByid(id);
        return a;

    }

    @RequestMapping("denglu")
    @ResponseBody
    public Map<String, Object> selectBY(String uname, String password) {
        Map<String, Object> map = userService.selectBynamepassword(uname, password);
        return map;
    }

    @RequestMapping("zhuche")
    public String zhuche() {
        return "user/zhuche";
    }

    @RequestMapping("selectuname")
    @ResponseBody
    public boolean selectuname(String uname) {
        boolean a = userService.selectBname(uname);
        return a;
    }

    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(User y) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        boolean a = userService.insertInfo(y);
        return a;
    }
}
